<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域 -->
      <div class="avatar_box">
        <img src="../assets/images/avatar.png" alt />
      </div>
      <!-- 登录表单区域 -->
      <el-form
        ref="loginFormRef"
        label-width="0px"
        :model="loginForm"
        :rules="loginFormRules"
        class="Form_box"
      >
        <!-- 账号 -->
        <el-form-item prop="username">
          <el-input prefix-icon="el-icon-user" v-model="loginForm.username"></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input type="password" prefix-icon="el-icon-lock" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <el-form-item class="btn_box">
          <el-button type="primary" @click="submitLoginForm">登录</el-button>
          <el-button @click="resetLoginForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data () {
    return {
      // 登录表单数据
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单验证规则
      loginFormRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'blur' },
          { min: 5, max: 10, message: '用户名长度为5-10个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '密码不能为空', trigger: 'blur' },
          { min: 6, max: 15, message: '密码长度为6-15个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 重置标表单数据
    resetLoginForm () {
      this.$refs.loginFormRef.resetFields()
    },
    // 提交表单数据
    submitLoginForm () {
      this.$refs.loginFormRef.validate(async vaild => {
        // eslint-disable-next-line no-useless-return
        if (!vaild) return
        const { data: res } = await this.$http.post('/login', this.loginForm)
        if (res.meta.status !== 200) return this.$message.error('登录失败，' + res.meta.msg)
        this.$message.success('登录成功，欢迎您 ' + res.data.username)
        // 登录成功后
        // 1.将token保存在sessionStorage中
        window.sessionStorage.setItem('token', res.data.token)
        // 2.跳转导航
        this.$router.push('/home')
      })
    }

  }
}
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  position: relative;

  .login_box {
    width: 450px;
    height: 300px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    box-shadow: 0px 0px 15px;

    .avatar_box {
      width: 130px;
      height: 130px;
      border: 2px solid #eee;
      box-shadow: 0 0 10px;
      border-radius: 50%;
      overflow: hidden;
      margin-left: 50%;
      transform: translateX(-65px);
      margin-top: -65px;

      img {
        width: 100%;
        height: 100%;
      }
    }
  }
}

.Form_box {
  position: absolute;
  bottom: 0px;
  width: 100%;
  padding: 0 20px;
  box-sizing: border-box;

  .btn_box {
    display: flex;
    justify-content: space-between;
  }
}
</style>
